<template>
  <!-- 能效分析-能源概况 -->
  <div class='app-container'>
    <el-card shadow='never'>
      <el-form ref='form' class='box-card' :inline='true' :model='form'>
        <div class='formtop'>
          <el-form-item label='周期'>
            <el-radio-group v-model='dateType' size='mini'>
              <el-radio-button label='日' />
              <el-radio-button label='月' />
              <el-radio-button label='年' />
            </el-radio-group>
          </el-form-item>
          <el-form-item label='日期'>
            <el-date-picker
              ref='picker'
              v-model='form.end_time'
              size='mini'
              :type='parseDate()'
              placeholder='选择日期'
              style='width: 100%'
            />
          </el-form-item>

          <!-- <el-form-item label="" label-width="0">

            <div class="formRight">
              <template v-for="item in tabData">
                <div
                  style="cursor: pointer"
                  :class="item.id === form.type_id ? 'active' : ''"
                  @click="handleType(item.id)"
                >
                  {{ item.type_name }}
                </div>
              </template>
            </div>
          </el-form-item> -->
          <el-form-item>
            <el-select
              v-model='value'
              filterable
              placeholder='请选择'
              size='mini'
              style='margin-right: 20px'
              @change='optionChange'
            >
              <el-option
                v-for='item in selectOptions'
                :key='item.id'
                :label='item.name'
                :value='item.id'
              />
            </el-select>
          </el-form-item>
          <el-form-item label='' label-width='0'>
            <el-button
              class='h50'
              size='mini'
              type='primary'
              icon='el-icon-search'
              @click='getChartData'
            />
          </el-form-item>
        </div>
      </el-form>
    </el-card>
    <div style='margin-top: 20px'>
      <!-- 柱状图 -->
      <div class='title'><span>用电耗能概况</span></div>
      <div ref='myChart1' id='myChart1' class='chart' />
      <!-- 柱状图 -->
      <div class='title' style='margin-top: 20px'>
        <span>24小时负荷趋势</span>
      </div>
      <div ref='myChart2' id='myChart2' class='chart' />
    </div>
  </div>
</template>
<script>
import { electricityParameters, monitorChartData } from '@/api/realTime.js'
import { stationAllList } from '@/api/fileManage.js'
import { getSurveyInfos, getSurveyInfoes } from '@/api/overView.js'
import * as echarts from 'echarts'

export default {
  name: 'index',
  data() {
    return {
      value: '',
      dateType: '日', // 日期得格式
      tabData: [], // tab数据
      typeId: null, // tabId
      tableData: [],
      options: [],
      choseData: {},
      form: {
        end_time: '',
        monitor_id: '',
        param_id: [],
        type_id: null,
      },
      selectOptions: [],
      //  图表数据
      option1: {
        title: {
          text: '',
        },
        tooltip: {
          trigger: 'axis',
        },
        toolbox: {
          show: true,
          feature: {
            dataZoom: {
              yAxisIndex: 'none',
            },
            dataView: { readOnly: false },
            magicType: { type: ['bar', 'line'] },
            restore: {},
            saveAsImage: {
              show: true,
              icon: 'M4.7,22.9L29.3,45.5L54.7,23.4M4.6,43.6L4.6,58L53.8,58L53.8,43.6M29.2,45.1L29.2,0',
              title: '保存为图片',
              type: 'png',
              connectedBackgroundColor: '#fff',
              name: '',
              excludeComponents: ['toolbox'],
              lang: ['右键另存为图片'],
            },
          },
        },
        axisPointer: {
          link: [
            {
              xAxisIndex: 'all',
            },
          ],
        },
        dataZoom: [],
        xAxis: {
          type: 'category',
          // boundaryGap: false,
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            formatter: '{value} KWh',
          },
        },
        series: [
          {
            name: '用电耗能概况',
            type: 'bar',
            barWidth: 20,
            data: [10, 11, 13, 11, 12, 12, 9],
            markPoint: {
              data: [
                { type: 'max', name: 'Max' },
                { type: 'min', name: 'Min' },
              ],
            },
          },
        ],
      },
      option2: {
        title: {
          text: '',
        },
        tooltip: {
          trigger: 'axis',
        },
        toolbox: {
          show: true,
          feature: {
            dataZoom: {
              yAxisIndex: 'none',
            },
            dataView: { readOnly: false },
            magicType: { type: ['bar', 'line'] },
            restore: {},
            saveAsImage: {
              show: true,
              icon: 'M4.7,22.9L29.3,45.5L54.7,23.4M4.6,43.6L4.6,58L53.8,58L53.8,43.6M29.2,45.1L29.2,0',
              title: '保存为图片',
              type: 'png',
              connectedBackgroundColor: '#fff',
              name: '',
              excludeComponents: ['toolbox'],
              lang: ['右键另存为图片'],
            },
          },
        },
        axisPointer: {
          link: [
            {
              xAxisIndex: 'all',
            },
          ],
        },
        dataZoom: [],
        xAxis: {
          type: 'category',
          // boundaryGap: false,
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            formatter: '{value} KW',
          },
        },
        series: [
          {
            name: '24小时负荷趋势',
            type: 'line',
            // type: 'bar',
            barWidth: 20,
            data: [10, 11, 13, 11, 12, 12, 9],
            markPoint: {
              data: [
                { type: 'max', name: 'Max' },
                { type: 'min', name: 'Min' },
              ],
            },
          },
        ],
      },
    }
  },
  watch: {
    // 树形数据
    choseData(nVal, oVal) {
      this.form.monitor_id = nVal.id
    },
    dateType(nVal, oVal) {
      this.form.end_time = this.getTime(this.form.end_time)
      this.getSurvey()
      this.getSurveys()
    },
  },
  created() {
    // 获取当前时间
    this.form.end_time = this.getDay(0)
    this.getStation()
    // this.getSurvey()
  },
  mounted() {
  },
  methods: {
    // 解析格式
    parseDate() {
      return this.dateType === '日'
        ? 'date'
        : this.dateType === '月'
          ? 'month'
          : 'year'
    },
    // 顶部搜索
    getChartData() {
      this.form.end_time = this.getTime(this.form.end_time)
      this.getSurvey()
      this.getSurveys()
    },
    // 底部下拉框选中
    optionChange(num) {
      console.log('选中的数据', num)
      this.form.end_time = this.getTime(this.form.end_time)
      this.value = num
      // this.getSurveys()
    },
    // 获取顶部参数查询
    getTopSearch() {
      if (this.choseData.id === undefined) return
      electricityParameters(this.choseData.id, 0).then((res) => {
        this.tabData = res.data.data
        this.options = res.data.child
        if (this.tabData.length > 0) {
          this.form.type_id = this.tabData[0].id
          this.getChartData()
        }
      })
    },
    // 日期格式化
    getTime(date) {
      var t = new Date(date)
      var year = t.getFullYear()
      var month = t.getMonth() + 1
      var day = t.getDate()

      var newTime =
        year +
        '-' +
        (month < 10 ? '0' + month : month) +
        '-' +
        (day < 10 ? '0' + day : day)
      return newTime
    },
    /**
     * @description: 获取当前日期
     * @author: hst
     * @param: day: 0今天，-1昨天
     */
    getDay(day) {
      var today = new Date()
      var targetday = today.getTime() + 1000 * 60 * 60 * 24 * day
      today.setTime(targetday)
      var tYear = today.getFullYear()
      var tMonth = today.getMonth()
      var tDate = today.getDate()
      tMonth = this.doHandleMonth(tMonth + 1)
      tDate = this.doHandleMonth(tDate)
      return tYear + '-' + tMonth + '-' + tDate
    },
    doHandleMonth(month) {
      var m = month
      if (month.toString().length === 1) {
        m = '0' + month
      }
      return m
    },
    // 获取用电耗能图表数据
    getSurvey() {
      const data = {}
      if (this.dateType == '日') {
        data.period_type = 'day2'
      } else if (this.dateType == '月') {
        data.period_type = 'month2'
      } else {
        data.period_type = 'year'
      }
      data.day_time = this.form.end_time
      data.station_id = this.value
      console.log('初始化calueeee', this.value)

      getSurveyInfos(data).then((res) => {
        this.option1.xAxis.data = res.data.table[0].name
        this.option1.series[0].data = res.data.table[0].data
        const chart = this.$refs.myChart1
        if (chart) {
          this.$echarts.dispose(document.getElementById('myChart1'))
          const myChart = this.$echarts.init(chart)
          myChart.setOption(this.option1)
          window.addEventListener('resize', () => {
            myChart.resize()
          })
        }
      })
    },
    // 获取24H趋势图表数据
    getSurveys() {
      const data = {}
      if (this.dateType == '日') {
        data.period_type = 'day2'
      } else if (this.dateType == '月') {
        data.period_type = 'month2'
      } else {
        data.period_type = 'year'
      }
      data.day_time = this.form.end_time
      data.station_id = this.value
      console.log('初始化calue', this.value)
      getSurveyInfoes(data).then((res) => {
        this.option2.xAxis.data = res.data.table[0].name
        this.option2.series[0].data = res.data.table[0].data
        const chart = this.$refs.myChart2
        if (chart) {
          this.$echarts.dispose(document.getElementById('myChart2'))
          const myChart = this.$echarts.init(chart)
          myChart.setOption(this.option2)
          window.addEventListener('resize', () => {
            myChart.resize()
          })
        }
      })
    },
    /**
     * @description: 获取站点
     * @author: hst
     * @param: day: 0今天，-1昨天
     */
    getStation() {
      stationAllList().then((res) => {
        this.selectOptions = res.data.data
        // console.log('获取下拉框', this.selectOptions[0])
        this.value = this.selectOptions[0].id
        this.getSurveys()
        this.getSurvey()
      })
    },
  },
}
</script>

<style lang='scss' scoped>
.app-container {
  /*background-color: #fff;*/
}

::v-deep .tableHead {
  background-color: #edf1f8 !important;
}

.mp30 {
  padding: 30px;
}

.h50 {
  height: 28px;
}

.box-card {
  height: 30px;
  /*background-color: #ffffff;*/
}

.topData {
  display: flex;
  justify-content: space-around;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
  width: 100%;
  height: 45px;
  border-radius: 10px;
  border: 1px solid #e4e4e4;
}

.formtop {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  height: 50px;
}

.formRight {
  overflow-x: auto;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;

  div {
    height: 35px;
    margin: 0 10px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #838ea0;
  }

  .active {
    height: 35px;
    background: linear-gradient(90deg, #3494ff, #006dff);
    box-shadow: 0px 6px 12px 0px rgba(35, 113, 255, 0.29);
    border-radius: 17px;
    color: #ffffff;
    padding: 0px 25px;
  }
}

.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 40px;
  /*text-align: center;*/
  border-radius: 10px 10px 0px 0px;
  background-color: #fff;
  border-bottom: 1px solid #eaeaea;

  span {
    font-size: 16px;
    color: #287b85;
    text-indent: 2em;
  }
}

.chart {
  width: 100%;
  height: 364px;
  border-radius: 0px 0px 10px 10px;
  background-color: #fff;
  overflow: hidden;
}

.radius {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 333px;
  border-radius: 10px;
  border: 1px solid #e4e4e4;

  div {
    flex: 1;
    height: 100%;
  }
}

.tableBox {
  width: 100%;
  margin: 20px auto;
}

.warningBox {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: 1140px;
  height: 54px;
  border: 1px solid #cecdcd;
  background: #f1f1f1;
  border-radius: 10px;
  font-size: 14px;
  padding: 15px;
  margin: 10px auto;

  .left {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    background-color: #333333;
    margin-right: 10px;
  }

  .right {
    font-size: 14px;
  }
}
</style>
